<?php
function msg($code, $msg = null, $data = array()) {
	$arr = array(
		'code' => $code
	);
	if ($msg !== null)
		$arr['msg'] = $msg;
	
	if (!empty($data) && is_array($data))
		$arr = array_merge($arr, $data);
	
	return $arr;
}

//数组转json字符串
function json($arr = array(), $pre = true) {
	return str_replace('\/', '/', phpversion() < 5.4 ? json_encode($arr) : json_encode($arr, $pre ? 128 | 256 : 256));
}

// 删除文件夹
function deldir($dir) {
	if (!file_exists($dir))
		return true;
	
	$dh = opendir($dir);
	while ($file = readdir($dh)) {
		if ($file != "." && $file != "..") {
			$fullpath = $dir . "/" . $file;
			if (!is_dir($fullpath)) {
				unlink($fullpath);
			} else {
				deldir($fullpath);
			}
		}
	}
	closedir($dh);
	return rmdir($dir);
}

// 验证手机号码
function isMobile($mobile) {
	return preg_match('/^(1(([38]\d)|(4[5-9])|(5[0-35-9])|66|(7[0-8])|(9[18-9]))\d{8})$/', $mobile);
}

//获取客户端真实ip地址
function getip() {
	$ip = null;
	if (isset($_SERVER['HTTP_CLIENT_IP'])) {
		$ip = $_SERVER['HTTP_CLIENT_IP'];
	} else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
		$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
	} else {
		$ip = $_SERVER['REMOTE_ADDR'];
	}
	return $ip;
}

/**
 * [addCookie 添加Cookie]
 * @param [type] $name   [Cookie名称]
 * @param string $value  [Cookie值]
 * @param [type] $expire [有效时间，秒]
 */
function addCookie($name, $value = '', $expire = null) {
	setcookie($name, $value, is_numeric($expire) ? time() + $expire : null, dirname($_SERVER['SCRIPT_NAME']), $_SERVER['HTTP_HOST']);
}

/**
 * [delCookie 删除Cookie]
 * @param  [type] $name [Cookie名称]
 */
function delCookie($name) {
	if (isset($_COOKIE[$name]))
		setcookie($name, '', time() - 3600, str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])), $_SERVER['HTTP_HOST']);
}

/**
 * [autoSize 大小单位自动转换]
 * @param  [type] $size      [字节大小]
 * @param  string $separator [连接符]
 * @return [type]            [带单位的大小]
 */
function autoSize($size, $separator = ' ') {
	$sz     = ' KMGTP';
	$factor = floor((strlen($size) - 1) / 3);
	return ($size >= 1024) ? sprintf("%.2f", $size / pow(1024, $factor)) . $separator . @$sz[$factor] : $size;
}

/**
 * [array2tree 一维数组转父子结构多维数组]
 * @param  [type] $list   [一维数组数据]
 * @param  array  $option [默认父子字段名称、顶级id等选项]
 * @return [type]         [父子结构多维数组]
 */
function array2tree($list, $option = array()) {
	$c = array(
		'id' => 'id',
		'pId' => 'pId',
		'child' => 'child',
		'root' => null
	);
	if (is_array($option))
		$c = array_merge($c, $option);

	$tree = array();
	if (is_array($list)) {
		$refer = array();
		foreach ($list as $key => $data)
			$refer[$data[$c['id']]] =& $list[$key];
		
		foreach ($refer as $key => $data) {
			$parentId = $data[$c['pId']];
			
			if ($c['root'] === $parentId) {
				$tree[$data[$c['id']]] =& $refer[$key];
			} else if (isset($refer[$parentId])) {
				$parent =& $refer[$parentId];
				$parent[$c['child']][$data[$c['id']]] =& $refer[$key];
				$parent[$c['child']] = array_values($parent[$c['child']]);
			}
		}
		
	}
	return array_values($tree);
}

/**
 * [filter_post 过滤POST参数]
 * @param  array   $fields [过滤参数字段]
 * @param  boolean $empty  [是否删除空值]
 * @return [type]          [过滤后的字段]
 */
function filter_post($fields = array(), $empty = false) {
	$items = array();
	foreach ($_POST as $key => $value)
		if (isset($fields[$key]) && (!$empty || trim($value) !== '')) {
			$items[]     = $fields[$key];
			$_POST[$key] = trim($value);
		} else
			unset($_POST[$key]);
	
	return $items;
}